import 'package:yysk_app/model/address/address_node.dart';
import 'package:yysk_app/model/collection_model/get_qualification_list_model.dart';
import 'package:yysk_app/model/common_model/mine_points_model.dart';
import 'package:yysk_app/model/common_model/notice_label_list.dart';
import 'package:yysk_app/model/pay_model/bank_card_list_model.dart';
import 'package:yysk_app/model/pay_model/cashier_route_query_model.dart';
import 'package:yysk_app/model/pay_model/list_bank_branch_hf_model.dart';
import 'package:yysk_app/model/pay_model/list_bank_hf_model.dart';
import 'package:yysk_app/model/pay_model/user_list_pay_certification_model.dart';
import 'package:yysk_app/model/user_friend/user_friend_list_model.dart';
import 'package:yysk_app/model/user_prop_model/list_by_propId_send_model.dart';
import 'package:yysk_app/utils/js_tool.dart';

class Pagination {
  final int pageNum;
  final int pageSize;
  final int total;
  final int totalPage;

  Pagination({
    required this.pageSize,
    required this.total,
    required this.totalPage,
    required this.pageNum,
  });

  factory Pagination.fromJson(Map<String, dynamic> json) {
    return Pagination(
      pageNum: json['pageNum'] as int,
      pageSize: json['pageSize'] as int,
      total: json['total'] as int,
      totalPage: json['totalPage'] as int,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'pageNum': pageNum,
      'pageSize': pageSize,
      'total': total,
      'totalPage': totalPage,
    };
  }
}

class ResponseData<T> {
  String? message;
  int? code;
  int? levelId;
  bool? successful;
  T? data;
  T? result;
  List<T>? list;
  Pagination? pagination;

  ResponseData({
    this.message,
    this.list,
    this.levelId,
    this.code,
    this.data,
    this.result,
    this.successful,
    this.pagination,
  });

  ResponseData.fromJson(
    Map<String, dynamic> json,
    T Function(Map<String, dynamic>) fromJson,
  ) {
    message = json['message'];
    code = json['code'];
    successful = json['successful'];
    levelId = json['levelId'];

    if (json.containsKey('data') && json['data'] != null) {
      if (json['data'] is List) {
        if (json['data'].every((element) => element is Map<String, dynamic>)) {
          List<Map<String, dynamic>> mapList =
              json['data'].cast<Map<String, dynamic>>();
          data = fromJsonListAsT<T>(mapList);
        } else {
          data = json['data'];
        }
      } else if (json['data'] is Map<String, dynamic>) {
        data = fromJson(json['data']);
      } else {
        data = json['data'];
      }
    } else {
      data = null;
    }
    if (json.containsKey('result') && json['result'] != null) {
      if (json['result'] is List) {
        if (json['result'].every(
          (element) => element is Map<String, dynamic>,
        )) {
          List<Map<String, dynamic>> mapList =
              json['result'].cast<Map<String, dynamic>>();
          result = fromJsonListAsT<T>(mapList);
        } else {
          result = json['result'];
        }
      } else if (json['result'] is Map<String, dynamic>) {
        result = fromJson(json['result']);
      } else {
        result = json['result'];
      }
    } else {
      result = null;
    }
  }
  static M? fromJsonListAsT<M>(List<Map<String, dynamic>> json) {
    //公告列表
    if (M == List<NoticeLabelItem>) {
      return NoticeLabelItem.fromList(json) as M;
    }
    //开户列表
    if (M == List<UserListPayCertificationModel>) {
      return UserListPayCertificationModel.fromList(json) as M;
    }
    //支付模块
    if (M == List<CashierRouteQueryData>) {
      return CashierRouteQueryData.fromList(json) as M;
    }
    //积分模块BankCardListModel
    if (<MinePointListModel>[] is M) {
      return MinePointListModel.fromList(json) as M;
    }
    if (M == List<AddrList>) {
      return AddrList.fromList(json) as M;
    }

    //银行卡列表
    if (M == List<BankCardListModel>) {
      return BankCardListModel.fromList(json) as M;
    }
    //ListByPropIdSendModel
    if (M == List<ListByPropIdSendModel>) {
      return ListByPropIdSendModel.fromList(json) as M;
    }
    if (M == List<ListBankHfModel>) {
      return ListBankHfModel.fromList(json) as M;
    }
    if (M == List<ListBankBranchHfModel>) {
      return ListBankBranchHfModel.fromList(json) as M;
    }
    if (M == List<GetQualificationListModel>) {
      return GetQualificationListModel.fromList(json) as M;
    }

    consoleLog('即将返回null${M}');
    return null;
  }

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['code'] = code;
    map['message'] = message;
    map['data'] = data;
    map["result"] = result;
    map['successful'] = successful;
    map['list'] = list;
    map['levelId'] = levelId;
    map['pagination'] = pagination;
    return map;
  }

  @override
  String toString() {
    return 'ResponseData{message: $message, code: $code, data: $data, successful: $successful}';
  }
}
